import javax.microedition.lcdui.Graphics;

#include "defines.h"
class Menu
{
	int x;
	int y;
	
	int height;
	int width;
	
	int bgcolor_start;
	int bgcolor_end;
	int hlcolor;
	int fgcolor;
	int borderW;
	
	int scr_w;
	int scr_h;
	
	long lastInput;
	
	int lineSpacing;
	
	int w_offset;
	int h_offset;
	
	int selIndex;
	int selLimitLow;
	
	int alphaLevel = 0;
	
	DSmallFont font; 
	Graphics g;
	
	String[] entrys;
	short[] entryType;
	short[] entryState;
	int[] maxValues;
	int[] minValues;
	
	public static final int TYPE_STANDARD = 0;
	public static final int TYPE_VALUE = TYPE_STANDARD + 1;
	public static final int TYPE_ON_OFF = TYPE_VALUE + 1;
	
	Menu()
	{
	}
	
	Menu(int _sw, int _sh,int _bgc, int _fc, int _hl, int _sll, String[] _e, Graphics _g)
	{
		int maxStrSize = 0;
		
		scr_w = _sw;
		scr_h = _sh;
		
		g = _g;
		
		bgcolor_start = _bgc;
		bgcolor_end = _bgc;
		hlcolor = _hl;
		fgcolor = _fc;
		
		selLimitLow = _sll;
		
		if(font==null)
			font = new DSmallFont(g, scr_h);
			
		font.setColor(fgcolor);
		font.setPixelSize(scr_w / 120);
		
		entrys = _e;
		entryType = new short[entrys.length];
		entryState = new short[entrys.length];
		maxValues = new int[entrys.length];
		minValues = new int[entrys.length];
		lineSpacing = font.pixelSize * 3;
		
		w_offset = 4;
		h_offset = 10;
		selIndex = 2;
		
		lastInput = 0;
		
		for(int i = 0; i < entrys.length; i++)
		{
			if(font.getStringWidth(entrys[i] + ": OFF") > maxStrSize)
			{
				maxStrSize = font.getStringWidth(entrys[i] + ": OFF");
			}
		}
		
		
		x = (scr_w / 2) - (maxStrSize / 2) - w_offset;
		height = ((entrys.length - 1) * lineSpacing) + (entrys.length * font.getFontHeight()) + (h_offset * 2);
		y = scr_h / 2 - height / 2;
		width = maxStrSize + (w_offset * 2);
	}
	
	Menu(int _sw, int _sh, int _bgc, int _fc, int _hl, int _sll, String[] _e, Graphics _g, DSmallFont _fnt)
	{
		int maxStrSize = 0;
		
		scr_w = _sw;
		scr_h = _sh;
		
		g = _g;
		
		bgcolor_start = _bgc;
		bgcolor_end = _bgc;
		hlcolor = _hl;
		fgcolor = _fc;
		
		selLimitLow = _sll;
		
		font = _fnt;
		
		if(font==null)
			font = new DSmallFont(g, scr_h);
			
		font.setColor(fgcolor);
		//font.setPixelSize(scr_w / 120);
		font.setPixelSize(2);
		
		entrys = _e;
		entryType = new short[entrys.length];
		entryState = new short[entrys.length];
		maxValues = new int[entrys.length];
		minValues = new int[entrys.length];
		lineSpacing = font.pixelSize * 3;
		
		w_offset = 4;
		h_offset = 10;
		selIndex = _sll;
		
		lastInput = 0;
		
		for(int i = 0; i < entrys.length; i++)
		{
			if(font.getStringWidth(entrys[i] + ": OFF") > maxStrSize)
			{
				maxStrSize = font.getStringWidth(entrys[i] + ": OFF");
			}
		}
		
		
		x = (scr_w / 2) - (maxStrSize / 2) - w_offset;
		height = ((entrys.length - 1) * lineSpacing) + (entrys.length * font.getFontHeight()) + (h_offset * 2);
		y = scr_h / 2 - height / 2;
		width = maxStrSize + (w_offset * 2);
	}	
	
	public void setValueEntry(int entryIndex, short minValue, short maxValue)
	{
		entryType[entryIndex] = TYPE_VALUE;
		entryState[entryIndex] = minValue;
		minValues[entryIndex] = minValue;
		maxValues[entryIndex] = maxValue;
	}
	
	public void setValueEntry(int entryIndex, int minValue, int maxValue)
	{
		setValueEntry(entryIndex, (short)minValue, (short)maxValue);
	}
	
	public void setOnOffEntry(int entryIndex, boolean state)
	{
		entryType[entryIndex] = TYPE_VALUE;
		entryState[entryIndex] = ((state)? (short)1 : (short)0);
		minValues[entryIndex] = 0;
		maxValues[entryIndex] = 1;
	}
	
	public void setOnOffState(int entryIndex, boolean state)
	{
		entryState[entryIndex] = ((state)? (short)1 : (short)0);
	}
	
	public int getEntryState(int entryIndex)
	{
		return entryState[entryIndex];
	}
	
	public void handleInput(int keyFlags)
	{			
		if(System.currentTimeMillis() - lastInput > KEYS.INPUT_THRESHOLD)
		{
			if(((keyFlags & KEYS.F_KEY_UP) != 0) &&
			(selIndex > selLimitLow))
			{
				selIndex--;
			}
			else if(((keyFlags & KEYS.F_KEY_DOWN) != 0) &&
			(selIndex < entrys.length - 1))
			{
				selIndex++;
			}
			else if(((keyFlags & KEYS.F_KEY_LSK)!= 0) && entryType[selIndex]==TYPE_ON_OFF)
			{
				entryState[selIndex] = ((entryState[selIndex]==1)? (short)0 : (short)1);
			}
			else if(((keyFlags & KEYS.F_KEY_LEFT) != 0) && entryType[selIndex]==TYPE_VALUE && entryState[selIndex] > minValues[selIndex])
			{
				entryState[selIndex]--;
			}
			else if(((keyFlags & KEYS.F_KEY_RIGHT) != 0) && entryType[selIndex]==TYPE_VALUE && entryState[selIndex] < maxValues[selIndex])
			{
				entryState[selIndex]++;
			}
			
			lastInput = System.currentTimeMillis();
		}
	}
	
	void setFade(int color1, int color2)
	{
		bgcolor_start = color1;
		bgcolor_end = color2;
	}
	
	void setAlpha(int _ap)
	{
		alphaLevel = _ap;
	}
	
	public void draw()
	{
		draw(RECT_TYPE_PLAIN);
	}
	
	public static final int RECT_TYPE_PLAIN = 0;
	public static final int RECT_TYPE_FADE = RECT_TYPE_PLAIN + 1;
	public static final int RECT_TYPE_ALPHA = RECT_TYPE_FADE + 1;
	
	public void draw(int rectType)
	{
		int startY = y + h_offset;
		
		switch(rectType)
		{
			case RECT_TYPE_PLAIN:
				g.setColor(bgcolor_start);
				g.fillRect(x, y, width, height);
			break;
			case RECT_TYPE_FADE:
				drawGFRect(x, y, width, height, bgcolor_start, bgcolor_end);
			break;
			case RECT_TYPE_ALPHA:
				#ifdef USE_DRAWRGB
				drawAlphaRect(x, y, width, height, bgcolor_start, alphaLevel, borderW);
				#else
				drawGFRect(x, y, width, height, bgcolor_start, bgcolor_start);
				#endif
			break;
		}
		
		for(int i = 0; i < entrys.length; i++)
		{
			if(selIndex != i)
				font.setColor(fgcolor);
			else
				font.setColor(hlcolor);
			
			if(entryType[i]==TYPE_STANDARD)
				font.drawString(entrys[i], scr_w/2, startY + (lineSpacing * i) + (font.getFontHeight() * i), DSmallFont.TOP | DSmallFont.HCENTER);
			else if(entryType[i]==TYPE_VALUE)
				font.drawString(entrys[i] + ": " + entryState[i], scr_w/2, startY + (lineSpacing * i) + (font.getFontHeight() * i), DSmallFont.TOP | DSmallFont.HCENTER);
			else
				font.drawString(entrys[i] + ": " + ((entryState[i]==1)? "ON" : "OFF"), scr_w/2, startY + (lineSpacing * i) + (font.getFontHeight() * i), DSmallFont.TOP | DSmallFont.HCENTER);
		}
	}
	
	void drawGFRect(int x, int y, int w, int h, int startColor, int endColor)
	{	
		int rs = startColor >>>	16;
		int gs = (startColor >>> 8) & 0x000000FF;
		int bs = startColor & 0x000000FF;
				
		int rRatio = ((rs - (endColor >>> 16)) / (h)) * -1;
		int gRatio = ((gs - ((endColor >>> 8) & 0x000000FF)) / (h)) * -1;
		int bRatio = ((bs - (endColor & 0x000000FF)) / (h)) * -1;
				
		for(int i = 0; i < h; i++)
		{
			g.setColor((rs << 16) | (gs << 8) | (bs));
			g.fillRect(x, y + i, w, 1);
			rs+=rRatio;
			gs+=gRatio;
			bs+=bRatio;
		}		
	}
	
	public void drawAlphaRect(int x, int y, int w, int h, int color, int alphaLevel, int borderWidth)
	{
		int[] rect = new int[w * h];
		
		g.setColor(color);
		for(int i = 0; i < borderW; i++)
		{
			g.drawRect(x - i, y - i, w + i * 2, h + i * 2);
		}
		
		for(int i = 0; i < rect.length; i++)
		{
			rect[i] = (alphaLevel << 24) | color;
		}
		
		g.drawRGB(rect, 0, w, x, y, w, h, true);
	}
}